Skip to content

Always return empty map instead of nil when conversion fails#283

Merged
sagikazarmark merged 1 commit intospf13:masterfrom
andig:patch-1
Sep 8, 2025
Merged

Always return empty map instead of nil when conversion fails#283
sagikazarmark merged 1 commit intospf13:masterfrom
andig:patch-1

Conversation

@andig
Copy link
Copy Markdown
Contributor

@andig andig commented Jul 10, 2025

Fix #282, partially reverts #269

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jul 10, 2025

CLA assistant check
All committers have signed the CLA.

@andig andig changed the title Always return empty map when conversion fails Always return empty map instead of nil when conversion fails Jul 14, 2025
@andig
Copy link
Copy Markdown
Contributor Author

andig commented Jul 16, 2025

Friendly ping @sagikazarmark would appreciate feedback/fix.

@sagikazarmark
Copy link
Copy Markdown
Collaborator

Well...it's not an easy question. The library is not consistent about return values. I guess I tried to follow a pattern with that change.

I have to look through the library to figure out what pattern is used the most.

But I'd say relying on a return value when an error is returned is generally not a good idea.

@ccoVeille
Copy link
Copy Markdown

ccoVeille commented Aug 19, 2025

Well...it's not an easy question. The library is not consistent about return values. I guess I tried to follow a pattern with that change.

I have to look through the library to figure out what pattern is used the most.

But I'd say relying on a return value when an error is returned is generally not a good idea.

Yes ... but ... no.

The bug in evcc comes from viper.GetStringMapString

That calls cast.ToStringMapString

And when you look at the code, you can see the code of cast is the one ignoring the error.

https://github.com/spf13/cast/blob/v1.9.2/zz_generated.go

So based on that, reverting the previous behavior is the least we can do.

So I agree with @andig

@ccoVeille
Copy link
Copy Markdown

I feel like tests should be added to prevent such issues in the future

https://github.com/spf13/cast/blob/v1.9.2/zz_generated.go

These generated files could be tested, but it would require to update

https://github.com/spf13/cast/blob/v1.9.2/generator%2Fmain.go

Or maybe to manually create zz_test.go to test the few ones that could fail

Or simply add tests to every single methods involved in this file.

I'm unsure what would be the thing to do. Maybe both.

@andig
Copy link
Copy Markdown
Contributor Author

andig commented Aug 20, 2025

Readme says

If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

I seem to recall it saying something about „useful zero value“, but can‘t find that anymore. So nil may be expected from that perspective.

I any case it is an unannounced, breaking behavior change causing bugs.

@sagikazarmark
Copy link
Copy Markdown
Collaborator

After sleeping on it, I don't actually see the harm in returning an empty map/slice upon error.

But I think it should be consistent across all functions:

  • I like the "useful zero value" concept, we should add it to the readme (and at the same time properly define the behavior)
  • Make sure that all functions returning a type with a nil zero value actually returns a "useful zero value"

@andig would you mind going through the library to make sure it's done?

@andig
Copy link
Copy Markdown
Contributor Author

andig commented Aug 20, 2025

Afaikt, maps are the odd outlier. I wouldn't care about nil slices since I'd need to len- check them anyway before accessing elements and I can always append. maps are really different in that a nil map cannot be appended to, i.e. keys added.

would you mind going through the library to make sure it's done?

I've had a look in the past and it's beyond my grasp, sorry.

If you really want have nil maps that's probably a fair choice in terms of consistency, but imho warrants a major version and announcement to not get hit in the face ;).

@sagikazarmark sagikazarmark merged commit fc73346 into spf13:master Sep 8, 2025
1 check was pending
@andig
Copy link
Copy Markdown
Contributor Author

andig commented Sep 8, 2025

Much appreciated, thank you!

@andig andig deleted the patch-1 branch September 8, 2025 17:02
@sagikazarmark
Copy link
Copy Markdown
Collaborator

I'm not sure how this passed CI, but tests are breaking all over now.

@sagikazarmark
Copy link
Copy Markdown
Collaborator

Fixed

@andig
Copy link
Copy Markdown
Contributor Author

andig commented Oct 13, 2025

I'm sorry, no idea :O

leclark added a commit to bir/iken that referenced this pull request Nov 19, 2025
chouetz pushed a commit to DataDog/cast that referenced this pull request Apr 14, 2026
Always return empty map instead of nil when conversion fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToStringMapString: Breaking change in 1.9.0

4 participants